﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using MySql.Data.MySqlClient;

namespace AwesomeEnginePC.SQL
{
    public enum SQLQueryResult
    {
        Sucessful,
        FailedToConnect,
        FailedToExecute,
        FailedToSaveToDataSet,
    }

    public abstract class ConnectorBase
    {

        public SqlConnection MsSqlConnectionString
        {
            get;
            set;
        }

        public MySqlConnection MySqlConnectionString
        {
            get;
            set;
        }

        public ConnectorBase(SqlConnection msSqlConnection)
        {
            MsSqlConnectionString = new SqlConnection(msSqlConnection.ConnectionString);
            MySqlConnectionString = ConvertToMySqlConnection(MsSqlConnectionString);
        }

        public ConnectorBase(MySqlConnection mySqlConnection)
        {
            MySqlConnectionString = new MySqlConnection(mySqlConnection.ConnectionString);
            MsSqlConnectionString = ConvertToMsSqlConnection(MySqlConnectionString);
        }

        public ConnectorBase(string hostName, string database, string userName, string password)
        {
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder.DataSource = hostName;
            builder.InitialCatalog = database;
            builder.UserID = userName;
            builder.Password = password;

            MsSqlConnectionString = new SqlConnection(builder.ConnectionString);
            MySqlConnectionString = ConvertToMySqlConnection(MsSqlConnectionString);
        }

        private MySqlConnection ConvertToMySqlConnection(SqlConnection msSqlConnection)
        {
            MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
            builder.Server = msSqlConnection.DataSource;
            builder.Database = msSqlConnection.Database;
            builder.UserID = new SqlConnectionStringBuilder(msSqlConnection.ConnectionString).UserID;
            builder.Password = new SqlConnectionStringBuilder(msSqlConnection.ConnectionString).Password;

            return new MySqlConnection(builder.ConnectionString);
        }

        private SqlConnection ConvertToMsSqlConnection(MySqlConnection mySqlConnection)
        {
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder.DataSource = mySqlConnection.DataSource;
            builder.InitialCatalog = mySqlConnection.Database;
            builder.UserID = new MySqlConnectionStringBuilder(mySqlConnection.ConnectionString).UserID;
            builder.Password = new MySqlConnectionStringBuilder(mySqlConnection.ConnectionString).Password;
            
            return new SqlConnection(builder.ConnectionString);
        }
    }
}
